package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;

import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.config.BMSHomeCagetory;
import keeto.com.vn.bms.model.CategoryBMSHomeModel;
import keeto.com.vn.bms.ui.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CategoryBMSHomeFragmentAdapter extends BaseAdapter {
	private LayoutInflater inflater = null;
	private ArrayList<CategoryBMSHomeModel> mArrayList;
	private Context ctx;
	private int Type;
	private int viewHeight;
	private GridView.LayoutParams mParams;

	public CategoryBMSHomeFragmentAdapter(Context context, int type, int vHeight) {
		this.ctx = context;
		this.inflater = LayoutInflater.from(context);
		this.Type = type;
		this.viewHeight = vHeight;
		initdata(ctx);
		reSizeView();
	}

	private void reSizeView() {

		int height;
		if (mArrayList.size() % 2 == 1) {
			height = 2 * viewHeight / (mArrayList.size() + 1);
		} else {
			height = 2 * viewHeight / (mArrayList.size());
		}

		if (height > 0) {
			this.mParams = new GridView.LayoutParams(
					RelativeLayout.LayoutParams.MATCH_PARENT, height);
		} else {
			this.mParams = new GridView.LayoutParams(
					RelativeLayout.LayoutParams.MATCH_PARENT,
					RelativeLayout.LayoutParams.MATCH_PARENT);
		}
	}

	private void initdata(Context context) {
		mArrayList = new ArrayList<CategoryBMSHomeModel>();
		switch (Type) {
		case IBMSConfig.KEY_TYPE_LEADERSHIP:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_support),
					R.drawable.category_support_selector,
					BMSHomeCagetory.ICON_SOPPORT));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));
			break;
		case IBMSConfig.KEY_TYPE_CUSTOMER:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));
			break;

		case IBMSConfig.KEY_TYPE_ADMIN:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_surgery),
					R.drawable.category_surgery_selector,
					BMSHomeCagetory.ICON_SURGERY));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_support),
					R.drawable.category_support_selector,
					BMSHomeCagetory.ICON_SOPPORT));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_admin),
					R.drawable.category_admin_selector,
					BMSHomeCagetory.ICON_ADMIN));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));
			break;

		case IBMSConfig.KEY_TYPE_TECHNICIAL:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_surgery),
					R.drawable.category_surgery_selector,
					BMSHomeCagetory.ICON_SURGERY));
			mArrayList.add(new CategoryBMSHomeModel());
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));
			break;
		case IBMSConfig.KEY_TYPE_MARKETING:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_surgery),
					R.drawable.category_surgery_selector,
					BMSHomeCagetory.ICON_SURGERY));
			mArrayList.add(new CategoryBMSHomeModel());
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));
			break;
		case IBMSConfig.KEY_TYPE_DOCTOR:
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_surgery),
					R.drawable.category_surgery_selector,
					BMSHomeCagetory.ICON_SURGERY));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_support),
					R.drawable.category_support_selector,
					BMSHomeCagetory.ICON_SOPPORT));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_info),
					R.drawable.category_info_selector,
					BMSHomeCagetory.ICON_INFO));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_promotion),
					R.drawable.category_promotion_selector,
					BMSHomeCagetory.ICON_PROMOTION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_education),
					R.drawable.category_education_selector,
					BMSHomeCagetory.ICON_EDUCATION));
			mArrayList.add(new CategoryBMSHomeModel(context
					.getString(R.string.category_seminar),
					R.drawable.category_seminar_selector,
					BMSHomeCagetory.ICON_SEMINAR));

			break;
		default:
			break;
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayList.size();
	}

	@Override
	public CategoryBMSHomeModel getItem(int position) {
		// TODO Auto-generated method stub
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.home_gridview_item, parent, false);
			holder = new ViewHolder();
			holder.rlView = (RelativeLayout) view
					.findViewById(R.id.rl_item_home_gridview);
			holder.imv_item_category = (ImageView) view
					.findViewById(R.id.imv_item_category);
			holder.tv_item_category = (TextView) view
					.findViewById(R.id.tv_item_category);
			holder.rlView.setLayoutParams(mParams);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		CategoryBMSHomeModel model = mArrayList.get(position);
		if (model != null) {
			if (model.getDrawableResource() > 0) {
				holder.imv_item_category.setImageResource(model
						.getDrawableResource());
				holder.imv_item_category.setVisibility(View.VISIBLE);
				holder.rlView.setBackgroundColor(ctx.getResources().getColor(
						R.color.gray_hover));
			} else {
				holder.imv_item_category.setVisibility(View.INVISIBLE);
				holder.rlView.setBackgroundColor(ctx.getResources().getColor(
						R.color.white));

			}
			if (model.getCategoryName() != null
					&& !model.getCategoryName().equals("")) {
				holder.tv_item_category.setText(model.getCategoryName());
				holder.tv_item_category.setVisibility(View.VISIBLE);
				holder.rlView.setBackgroundColor(ctx.getResources().getColor(
						R.color.gray_hover));

			} else {
				holder.tv_item_category.setVisibility(View.INVISIBLE);
			}
		}

		return view;
	}

	class ViewHolder {
		private RelativeLayout rlView;
		private ImageView imv_item_category;
		private TextView tv_item_category;
	}

}
